<?php

namespace app\admin\model;


/**
 * 栏目模型
 * Class InformationClassification
 * @package app\admin\model
 */
class ColumnPage extends Base
{
    protected $autoWriteTimestamp = true;//开启自动添加时间戳
    protected $updateTime = false;
    protected $auto = ["substation_name"];
    protected $insert = ['create_id','create_nick_name'];//添加插入

    //添加人ID
    protected function setCreateIdAttr()
    {
        return $this->admin["admin_id"];
    }

    //添加人名称
    protected function setCreateNickNameAttr()
    {
        return !empty($this->admin["nick_name"]) ? $this->admin["nick_name"] : $this->admin["admin_name"];
    }

    //auto，分站名称
    protected function setSubstationNameAttr($value,$data)
    {
        $Substation = new Substation();//分站类
        return $Substation->getFieldValue("substation_id", $data["substation_id"], "substation_name");
    }

    //获取栏目列表
    public function columnList($keyword = ""){
        return $this
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("page_name","like","%".$keyword."%");
                }
            })
            ->where(function ($q){
                if(!empty($this->substations)){//是否设置了当前网站
                    $q->where("substation_id",$this->substations);
                }else{
                    if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
                        $arr = [];
                        $Substation = new Substation();//分站类
                        $userList = $Substation->userList();//获取分站集合
                        foreach ($userList as $val){
                            $arr[] = $val['substation_id'];
                        }
                        $q->where("substation_id","in",$arr);
                    }
                }
            })
            ->order("substation_id ASC,sort DESC,create_time ASC")
            ->select();
    }

    //设置判断当前站点是否有该分类
    public function checkParentId($parent_id,$substation_id){
        if(!empty($parent_id)) {//上级分类不为空判断
            return $this
                ->where("page_id", $parent_id)
                ->where(function ($q) use ($substation_id) {
                    if (!empty($substation_id)) {
                        $q->where("substation_id",  $substation_id);
                    }
                })
                ->where(config("TableDeleteField"), 0)//获取没有删除的
                ->count();
        }
        return true;
    }

    //验证是否有权限操作
    public function checkSave($page_id,$substation_id){
        $is = [false,false];//验证标识
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
            $Substation = new Substation();//分站类
            $userList = $Substation->userList();//获取分站集合
            $classification_substation_id = $this->where("page_id",$page_id)->value("substation_id");//获取原分站ID
            foreach ($userList as $val){//遍历分站集合
                if(!empty($page_id)) {//修改验证
                    if ($classification_substation_id == $val["substation_id"]) {
                        $is[0] = true;
                    }
                }else{
                    //添加不验证
                    $is[0] = true;
                }
                if(!empty($substation_id)) {//添加修改验证
                    if ($substation_id == $val["substation_id"]) {
                        $is[1] = true;
                    }
                }else{
                    //删除不验证
                    $is[1] = true;
                }
            }
        }else{
            $is = [true,true];
        }
        return $is[0] === true && $is[1] === true ? true : false;
    }
}